package cn.css0209.applicationdemo190509.book;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.css0209.applicationdemo190509.R;
import cn.css0209.applicationdemo190509.book.Adapter.BookAdapter;
import cn.css0209.applicationdemo190509.book.DB.Dao.BookDAO;
import cn.css0209.applicationdemo190509.utils.StatusBarUtil;

public class BookActivity extends AppCompatActivity {


    private EditText et_name;
    private EditText et_phone;
    private ListView listView;
    private List<BookDo> students;
    private String name;
    private Unbinder bind;
    private String phone;
    private BookAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book3);
        bind = ButterKnife.bind(this);
        et_name = findViewById(R.id.et_bookName);
        et_phone = findViewById(R.id.et_bookPhone);
        listView = findViewById(R.id.book_list);

        topBar();
        iniAdapter();
    }
    private void iniAdapter() {
        adapter = new BookAdapter(this);
        selectData();
        listView.setAdapter(adapter);
    }

    private void selectData() {
        students = BookDAO.newInstance(this).select();
        if (students != null) {
            adapter.addAllData(students);
        }
    }

    @OnClick({R.id.book_add_btn, R.id.book_del_btn, R.id.book_find_btn, R.id.book_modify_btn})
    void OnClick(View view) {
        String result = "";
        switch (view.getId()) {
            case R.id.book_add_btn:
                name = et_name.getText().toString();
                phone = et_phone.getText().toString();
                result = BookDAO.newInstance(this).insert(name, phone);
                selectData();
                break;
            case R.id.book_del_btn:
                name = et_name.getText().toString();
                result = BookDAO.newInstance(this).delete(name);
                selectData();
                break;
            case R.id.book_modify_btn:
                name = et_name.getText().toString();
                phone = et_phone.getText().toString();
                result = BookDAO.newInstance(this).update(name, phone);
                selectData();
                break;
            case R.id.book_find_btn:
                selectData();
                if (students != null) {
                    adapter.addAllData(students);
                    result = "查询成功";
                } else {
                    result = "查询失败";
                }
                break;
        }
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
    }

    public void topBar() {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        //设置状态栏透明
//        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, false)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
    }
}
